/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import generated_facades.BanksFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import rates_analysis.RatesAnalyzerSessionBeanLocal;

/**
 *
 * @author aav
 */
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
public class TestServlet extends HttpServlet {
    @EJB
    private BanksFacade banksFacade;
    @EJB
    private RatesAnalyzerSessionBeanLocal ratesAnalyzerSessionBean;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            try {
                ratesAnalyzerSessionBean.updateRates();
                //out.println(Double.parseDouble(ratesAnalyzerSessionBean.analyze("http://www.sbrf.ru/voronezh/ru/person/currency/operations/", "<td style=\"vertical-align:middle;font-size:16px\">.+?</td><td class=\"img2\" style=\"padding-bottom:5px;vertical-align:middle;\"><img src=\"/common/img/0.gif\" width=\"5\" height=\"3\" alt=\"\"></td><td style=\"vertical-align:middle;font-size:16px\">(.+?)</td></tr><tr><td style=\"padding-top:3px; text-align:center;\"><a href=\"/voronezh/ru/quotes/currencies/timeline/index.php\\?qid190=2\">")));
            } catch (Exception ex) {
                out.println(ex.getMessage());
                ex.printStackTrace(out);
            }
                

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
            try {
                out.println(banksFacade.find(BigDecimal.ONE).getBankName());
            } catch (Exception ex) {
                out.println(ex.getMessage());
                ex.printStackTrace(out);
            }
            java.util.Date date = new java.util.Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            out.println(date);
            out.println("</body>");
            out.println("</html>");

        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
